package com.py.pythink.service.impl;

import com.py.pythink.config.SystemConfig;
import com.py.pythink.dao.UserLikeDao;
import com.py.pythink.dao.UserSendDao;
import com.py.pythink.entity.UserLike;
import com.py.pythink.entity.UserSend;
import com.py.pythink.service.intf.UserLikeService;
import com.py.pythink.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserLikeServiceImpl implements UserLikeService {
    @Autowired
    private UserLikeDao dao;
    @Autowired
    private UserSendDao userSendDao;

    @Override
    @Transactional
    public R djok(int fuid, int uid) {
        System.err.println("点赞行动启动");

        UserLike userLike = dao.getInstance(fuid,uid);
        System.err.println(userLike);
        if (userLike == null) {
            UserLike userLike1 = new UserLike();
            userLike1.setUid(uid);
            userLike1.setFuid(fuid);
            if (dao.insert(userLike1) > 0) {
                UserSend userSend = userSendDao.selectById(fuid);
                Integer count = userSend.getCount();
                userSend.setCount(count = count + 1);
                userSendDao.updateCount(count, fuid);
                return R.ok("用户第一次对这个帖子点赞");
            }
        } else if (userLike.getFlag() == SystemConfig.USER_LIKE_TPOST) {
            dao.udate(SystemConfig.USER_LIKE_POST, fuid);
            UserSend userSend = userSendDao.selectById(fuid);
            Integer count = userSend.getCount();
            userSend.setCount(count = count + 1);
            userSendDao.updateCount(count, fuid);
            return R.ok();
        } else if (userLike.getFlag() == SystemConfig.USER_LIKE_POST) {
            dao.udate(SystemConfig.USER_LIKE_TPOST, fuid);
            UserSend userSend = userSendDao.selectById(fuid);
            Integer count = userSend.getCount();
            userSend.setCount(count = count - 1);
            userSendDao.updateCount(count, fuid);
            return R.ok();
        } else {
            return R.fail("亲，网络异常");
        }

        return null;
    }



}
